<?php
	require('withdraw.doc.php');
	$app->post(
		'/withdraw/add',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['trademoney']) && isset($data['payway']) && isset($data['userid']) && isset($data['usertype'])){
				if($data['trademoney']<50){
					$echo['error'] = "最少提现金额：50.00";
					echo json_encode($echo);exit;
				}
				$tb = $data['usertype'] == 1 ? 'tb_member' : 'tb_coach';
				$member=$db2->select(1, $tb, '*', array('tbid' => $data['userid']));
				if(!empty($member['tbid'])){
					$account=$db2->select(1, 'tb_bind_bank', '*', array('userid' => $data['userid'],'usertype' => $data['usertype'],'paytype'=>$data['payway']));
					if(!empty($account)){
						if($member['balance']>=$data['trademoney']){
							$tradeid=getordernum("TR");
							$pay=array('type'=>3,'userid'=>$data['userid'],'usertype' => $data['usertype'],'trademoney'=>$data['trademoney'],'payway'=>$data['payway'],'tradeid'=>$tradeid);
							creattrade($pay);
						}else{
							$echo['error'] = "账户余额不足";
						}
					}else{
						$echo['error'] = "没有绑定提现账号";
					}
				}else{
					$echo['error'] = "用户不存在";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	$app->post(
		'/withdraw/delete',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['tradeid']) && isset($data['userid']) && isset($data['usertype'])){
				$tb = $data['usertype'] == 1 ? 'tb_member' : 'tb_coach';
				$member=$db2->select(1, $tb, '*', array('tbid' => $data['userid']));
				if(!empty($member['tbid'])){
					$trade=$db2->select(1, 'tb_trade', '*', array('tbid' => $data['tradeid']));
					if(!empty($trade['tbid'])){
						if($member['freeze']>=$trade['trademoney']){
							$money=$member['balance'];
							$trademoney=$trade['trademoney'];
							$db2->insert("tb_tradedetail",array("tradeid"=>$data['tradeid'],"b_money"=>$money,"a_money"=>$money+$trademoney,"trademoney"=>$trademoney,'#creatdate'=>'now()','type'=>5,'userid'=>$data['userid']));
							$db2->update($tb,array('balance[+]'=>$trademoney,'freeze[-]'=>$trademoney),array('tbid'=>$data['userid']));
						}else{
							$echo['error'] = "账户冻结资金不足";
						}
					}else{
						$echo['error'] = "提现记录不存在";
					}
				}else{
					$echo['error'] = "用户不存在";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	$app->get(
		'/withdraw/get',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['userid'])  && isset($data['usertype']) && isset($data['from']) && isset($data['to'])){
				$tb = $data['usertype'] == 1 ? 'tb_member' : 'tb_coach';
				$member=$db2->select(1, $tb, '*', array('tbid' => $data['userid']));
				if(!empty($member['tbid'])){
					$sqlwhere=array('userid' => $data['userid'],'usertype' => $data['usertype'],'type'=>3,'order'=>'tbid desc');
					$echo['count'] = $db2->select(2, 'tb_trade', '*', $sqlwhere);
					$sqlwhere['limit'] = array($data['from'], $data['to']);
					$trade=$db2->select(0, 'tb_trade', '*', $sqlwhere);
					$echo['data'] = $trade;
				}else{
					$echo['error'] = "用户不存在";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
?>